package com.xjtu.platform.answer.answer.dao;

import com.xjtu.platform.answer.answer.AnswerDto;
import com.xjtu.platform.answer.answer.OptionDto;
import com.xjtu.platform.answer.answer.QuestionDto;
import com.xjtu.platform.answer.answer.requestDto;
import com.xjtu.platform.common.CurrentUser;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;
import java.util.List;

@Mapper
public interface AnswerDao {
    @Select("select favor from user where user_id=#{user_id}")
    String findFavorById(String user_id);

    @Select("(select question_id from questions,requirements where questions.request_id = requirements.request_id " +
            "and  questions.count>0 and requirements.stage = 3 " +
            "and requirements.mode=#{favor} and rank = #{rank} and question_id not in " +
            "(select question_id from answers where user_id = #{userId}) ) " +
            "limit 0,1")
    String findQues(CurrentUser user);

    @Update("update questions set count=count-1 where question_id = #{question_id}")
    void decreaseCount(String question_id);

    @Update("update questions set count=count+1 where question_id = #{question_id}")
    void increaseCount(String question_id);

    @Select("(select question_id from questions,requirements where questions.request_id = requirements.request_id " +
            "and  questions.count>0 and requirements.stage = 3 " +
            "and rank = #{rank} and question_id not in " +
            "(select question_id from answers where user_id = #{userId}) ) " +
            "limit 0,1")
    String findDoableQuestion(CurrentUser user);

    @Select("select path from questions where " +
            "question_id = #{question_id}")
    String getPathByQuest(String question_id);

    @Select("select request_id from questions where " +
            "question_id = #{question_id}")
    String getRequestByQuest(String question_id);

    @Select("select question_content,mode from requirements where " +
            "request_id = #{request_id}")
    requestDto getQuestContentAndModeByRequestId(String request_id);

    @Select("select * from options where " +
            "request_id = #{request_id}")
    ArrayList<OptionDto> getOptions(String request_id);

    @Insert("insert into answers(record_id, user_id, question_id, ans) " +
            "values(#{record_id}, #{user_id}, #{question_id}, #{ans})")
    void insertAnswer(AnswerDto ans);

    @Select("select * from answers where question_id = #{question_id} and user_id = #{user_id} limit 0,1")
    AnswerDto findAnswer(AnswerDto answerDto);

    @Select("select * from answers where question_id = #{question_id} ")
    List<AnswerDto> getReferenceList(String question_id);

    @Update("update questions set ans = #{ans}, is_valid = 1 where question_id = #{question_id}")
    boolean confirm(AnswerDto answer);

    @Update("update questions set  is_valid = -1 where question_id = #{question_id}")
    boolean reject(AnswerDto answer);

    @Update("update questions set  rank = #{rank} where question_id = #{question_id}")
    boolean setQuesRank(QuestionDto ques);

    @Select("select role_id from user where user_id = #{user_id}")
    String getRoleID(String user_id);

    @Select("select count(*) from (select count(*) from answers where question_id = #{question_id}  group by ans) gyd")
    Integer checkAnsSame(String question_id);

    @Select("select count from questions where question_id = #{question_id}")
    Integer checkCount(String question_id);

    @Select("select count(*) from questions,requirements where questions.request_id = requirements.request_id " +
            "and questions.rank = 0 and requirements.request_id = #{request_id}")
    Integer findDoable(String request_id);

    @Update("update requirements set stage = #{stage} where request_id = #{request_id}")
    Boolean setRequestStage(requestDto requestDto);

    @Update("select credit from user where user_id = #{user_id}")
    Integer checkCredit(AnswerDto answer);

    @Update("update user set credit = #{credit} where user_id = #{user_id}")
    Boolean setCredit(AnswerDto answer);

    @Select("select count(*) from questions,requirements where questions.request_id = requirements.request_id " +
            "and questions.rank = 1 and requirements.request_id = #{request_id}")
    Integer findCheckAble(String request_id);
}
